/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogCambiarContrasenia.java
 *
 * Created on 17/04/2011, 09:30:30 PM
 */

package frontera.interfaz.dialogo;

import control.SIRHApp;
import control.ValidarAutenticacion;
import entidad.Usuario;
import javax.swing.JOptionPane;

/**
 *
 * @author juanffx
 */
public class DialogCambiarContrasenia extends javax.swing.JDialog {

    /** Creates new form DialogCambiarContrasenia */
    public DialogCambiarContrasenia(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        newClaveTF = new javax.swing.JPasswordField();
        oldClaveTF = new javax.swing.JPasswordField();
        AceptarCambioClaveBT = new javax.swing.JButton();
        CancelarCambioClaveBT = new javax.swing.JButton();
        RepContrLB = new javax.swing.JLabel();
        newClaveRTF = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cambiar Contraseña");
        setAlwaysOnTop(true);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/Changepass.png"))); // NOI18N

        jLabel2.setText("Ingrese su actual contraseña:");

        jLabel3.setText("Ingrese su nueva contraseña:");

        AceptarCambioClaveBT.setText("Aceptar");
        AceptarCambioClaveBT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AceptarCambioClaveBTActionPerformed(evt);
            }
        });

        CancelarCambioClaveBT.setText("Cancelar");
        CancelarCambioClaveBT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarCambioClaveBTActionPerformed(evt);
            }
        });

        RepContrLB.setText("Repita su nueva contraseña:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(oldClaveTF, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(RepContrLB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(newClaveRTF)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(AceptarCambioClaveBT)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(CancelarCambioClaveBT))
                            .addComponent(newClaveTF))))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(oldClaveTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(newClaveTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(RepContrLB)
                            .addComponent(newClaveRTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AceptarCambioClaveBT)
                    .addComponent(CancelarCambioClaveBT))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AceptarCambioClaveBTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AceptarCambioClaveBTActionPerformed
        usuario = SIRHApp.getInstance().getAdmPermisos().getUsuario();
       


        if( String.valueOf(newClaveTF.getPassword()).equals(String.valueOf(newClaveRTF.getPassword())))
        {
            String verificacionClave = SIRHApp.getInstance().getAdmUsuarios().verificarContraseña(usuario.getContraseña(), String.valueOf(oldClaveTF.getPassword()));
        
        if (!verificacionClave.equals("")) {
            JOptionPane.showMessageDialog(this, verificacionClave, "Cambio de contraseña", JOptionPane.ERROR_MESSAGE);
        } else {

            String resultadoOperacion = SIRHApp.getInstance().getAdmUsuarios().actualizarUsuario(usuario, String.valueOf(newClaveTF.getPassword()));
            if (resultadoOperacion.equals("")) {
                JOptionPane.showMessageDialog(this, "Se ha cambiado exitosamente la contraseña.", "Cambio de contraseña", JOptionPane.INFORMATION_MESSAGE);
                this.setVisible(false);
            } else if (!resultadoOperacion.equals("")) {
                JOptionPane.showMessageDialog(this, resultadoOperacion, "Cambio de contraseña", JOptionPane.ERROR_MESSAGE);
            }
        }
        }
        else{
            JOptionPane.showMessageDialog(this, "Asegurese que la nueva contraseña coincida con su verificación", "Revise la verificación", JOptionPane.ERROR_MESSAGE);
        }

       


    }//GEN-LAST:event_AceptarCambioClaveBTActionPerformed

    private void CancelarCambioClaveBTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarCambioClaveBTActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_CancelarCambioClaveBTActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogCambiarContrasenia dialog = new DialogCambiarContrasenia(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AceptarCambioClaveBT;
    private javax.swing.JButton CancelarCambioClaveBT;
    private javax.swing.JLabel RepContrLB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField newClaveRTF;
    private javax.swing.JPasswordField newClaveTF;
    private javax.swing.JPasswordField oldClaveTF;
    // End of variables declaration//GEN-END:variables
    private Usuario usuario;
}
